<template>
  <div id="bg">
    <span  style="position:relative;top: 80px;color: white;font-size: 30px">批发市场管理系统</span>
    <br>
    <span  style="position:relative;top: 80px;color: white;font-size: 30px">MarketSystem</span>
    <div id="login"  >
      <img id="img2" src="https://wx1.sinaimg.cn/mw690/ae5a84fbly1glm4pm24jbj20ty0kodki.jpg">
      <div class="login_input">
        <a-icon type="smile"  style="font-size: 20px;color: #166EB5; margin-right: 15px" />
        <input type="text"  v-model="loginForm.username" placeholder="用户名"/><br>
        <a-icon type="lock" theme="twoTone" style="font-size: 20px; margin-right: 15px" />
        <input type="text" v-model="loginForm.password" placeholder="密码"/><br>
        <a-icon type="user"  style="font-size: 20px;color: #166EB5; margin-right: 15px" />
          <select v-model="selected" >
            <option v-bind:value="selected">请选择身份</option>
            <option>管理员</option>
            <option>店铺主</option>
            <option>顾客</option>
          </select><br>

      <a-button @click="login" style="background-color: rgba(25,110,200,0.8);width: 200px;color: white" >登录</a-button>
      </div>
    </div>
  </div>
</template>

<script>
// eslint-disable-next-line no-unused-vars
import axios from 'axios'
import { mapMutations } from 'vuex'
export default {
  name: 'login',
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      }
    }
  },

  methods: {
    ...mapMutations(['changeLogin']),
    login () {
      let _this = this
      if (this.loginForm.username === '' || this.loginForm.password === '') {
        alert('账号或密码不能为空')
      } else {
        axios({
          method: 'post',
          url: this.baseURL + 'login.ctl',
          contentType: 'application/x-www-form-urlencoded',
          dataType: 'jsonp',
          data: _this.loginForm
        }).then(res => {
          this.loginForm = {
            username: this.loginForm.username,
            password: this.loginForm.password
          }
          console.log(res.data)
          _this.userToken = res.data.token
          // 将用户token保存到vuex中
          _this.changeLogin({ Authorization: _this.userToken })
          console.log(res.data)
          if (res.data.message === '用户名或密码不匹配') {
            alert('用户名或密码不匹配')
          } else if (res.data.message === '数据库操作异常') {
            alert('数据库操作异常')
          } else if (res.data.message === '网络异常') {
            alert('网络异常')
          } else {
            for (let item of res.data.roles) {
              if (this.selected === item.description && this.selected === '管理员') {
                _this.$router.push('/AdministratorHome')
                window.localStorage.setItem('userName', res.data.actor.name)
                alert('登陆成功')
                return
              } else if (this.selected === item.description && this.selected === '店铺主') {
                _this.$router.push('/shopOwnerHome')
                window.localStorage.setItem('userName', res.data.actor.name)
                alert('登陆成功')
              } else if (this.selected === item.description && this.selected === '顾客') {
                _this.$router.push('/CustomerHome')
                window.localStorage.setItem('userName', res.data.actor.name)
                alert('登陆成功')
                return
              } else {
                alert('身份不匹配')
              }
            }
          }
        }).catch(error => {
          alert('账号或密码错误')
          console.log(error)
        })
      }
    }
  }
}
</script>

<style scoped>
#bg{width:100% ;
  height: 750px;
  background-attachment: fixed;
}
#img2{
  width:380px;
  height:280px;
  margin-left: -250px;
  margin-top: 70px;
}
 #login{
   width: 700px;
   height: 400px;
   position:absolute;
   left:0;
   top:150px;
   right:0;
   bottom:0;
   margin:auto;
   z-index: 100;
   background-color: rgb(255, 255, 255);
   box-shadow:0 0 30px rgba(0, 0, 0, 0.4);
   text-align: center;
 }
 .login_input{
   margin-left: 400px;
   margin-top: -250px;
 }
 #login input{
   margin-bottom:40px;
   line-height: 25px;
   width:180px;
 }
 #login input:focus{
   box-shadow:0 0 10px rgba(0, 0, 0, 0.4);
 }
 #login select{
   line-height: 25px;
   width:180px;
   height:28px
 }
 #login button{
   width:180px;
   border-radius: 5px;
   margin-top: 30px;
   box-shadow:0 0 5px rgba(0, 0, 0, 0.4);
 }
</style>
